import { z } from "zod"; import { guarded, json, body } from "@/lib/api"; import { sql } from "@/lib/db"; export const GET = guarded<{ id: string }>(async (_req, { params }) => { const p = await sql`SELECT * FROM projects WHERE id = ${params.id}`; if (!p[0]) return json({ error: "Introuvable" }, { status: 404 }); const files = await sql`SELECT id, filename, mime, size_bytes, page_count, created_at FROM attachments WHERE project_id = ${params.id} ORDER BY created_at DESC`; const memories = await sql`SELECT id, category, content, status, created_at FROM memories WHERE project_id = ${params.id} ORDER BY created_at DESC`; const artifacts = await sql`SELECT id, title, type, language, version, created_at FROM artifacts WHERE project_id = ${params.id} AND saved ORDER BY created_at DESC`; return json({ project: p[0], files, memories, artifacts }); }); export const PATCH = guarded<{ id: string }>(async (req, { params }) => { const b = await body(req, z.object({ name: z.string().min(1).max(120).optional(), description: z.string().max(2000).nullable().optional(), instructions: z.string().max(20_000).nullable().optional(), defaultModel: z.string().nullable().optional(), color: z.string().max(20).nullable().optional(), icon: z.string().max(20).nullable().optional(), notes: z.string().max(100_000).nullable().optional(), toolPermissions: z.record(z.string(), z.unknown()).optional(), archived: z.boolean().optional() })); await sql`UPDATE projects SET name = COALESCE(${b.name ?? null}, name), description = CASE WHEN ${b.description !== undefined} THEN ${b.description ?? null} ELSE description END, instructions = CASE WHEN ${b.instructions !== undefined} THEN ${b.instructions ?? null} ELSE instructions END, default_model = CASE WHEN ${b.defaultModel !== undefined} THEN ${b.defaultModel ?? null} ELSE default_model END, color = CASE WHEN ${b.color !== undefined} THEN ${b.color ?? null} ELSE color END, icon = CASE WHEN ${b.icon !== undefined} THEN ${b.icon ?? null} ELSE icon END, notes = CASE WHEN ${b.notes !== undefined} THEN ${b.notes ?? null} ELSE notes END, tool_permissions = COALESCE(${b.toolPermissions ? sql.json(b.toolPermissions as never) : null}, tool_permissions), archived_at = CASE WHEN ${b.archived === undefined} THEN archived_at WHEN ${b.archived ?? false} THEN now() ELSE NULL END WHERE id = ${params.id}`; const p = await sql`SELECT * FROM projects WHERE id = ${params.id}`; return json({ project: p[0] }); }); export const DELETE = guarded<{ id: string }>(async (_req, { params }) => { await sql`DELETE FROM projects WHERE id = ${params.id}`; return json({ ok: true }); });